package com.test.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.struts.upload.FormFile;
import org.apache.velocity.texen.util.FileUtil;

public class UploadFileUtil {

	/**
	 * 统一上传文件
	 * 
	 * @param upFile
	 *            要上传的文件
	 * @param baseDir
	 *            文件父目录
	 * @param filter
	 *            过滤问卷后缀 格式为 jpg,gif,bmp 如果为空或为null则不判断
	 * @param fileSize
	 *            如果fileSize<=0则默认不判断 fileSize 已k为单位
	 * @return
	 * @return boolean true 上传成功
	 * @exception Exception
	 *                如果设置了文件后缀或者文件长度 则对相应的进行判断 如果文件不成功则返回上传错误
	 */

	public static String UpFile(FormFile upFile, String baseDir, String filter, int fileSize, boolean isReplace) throws Exception {
		if (upFile == null) {
			throw new Exception("请选择一个文件进行上传");
		}
		String fileName = upFile.getFileName();

		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

		// 判断文件后缀是否合法
		if (filter != null && !filter.equals("")) {

			String[] filterSuffix = filter.split(",");
			int suffixLength = filterSuffix.length;
			boolean isLegal = false;
			for (int suffixI = 0; suffixI < suffixLength; suffixI++) {
				if (filterSuffix[suffixI].equals(suffix)) {
					isLegal = true;
				}
			}
			if (!isLegal) {
				throw new Exception("上传的文件名不合法.");
			}
		}

		// 判断文件长度是否合法
		if (fileSize > 0) {
			int upFileSize = upFile.getFileSize() / 1024;
			if (upFileSize > fileSize) {
				throw new Exception("上传的文件长度过长");
			}
		}

		// 文件真实保存路径
		String upFilePath = baseDir + "/" + upFile.getFileName();

		if (!isReplace) {
			File f = new File(upFilePath);
			if (f.exists()) {
				return upFilePath;
			}
		}

		try {
			// 保存文件
			if (FileUploadUtil.saveFile(upFilePath, upFile.getFileData())) {
				return upFilePath;
			}
			return "";

		}
		catch (IOException e) {
			throw new Exception("上传文件错误,请重试");
		}
	}

	public static String UpZipFile(FormFile upFile, String baseDir, String filter, int fileSize, boolean isReplace) throws Exception {
		if (upFile == null) {
			throw new Exception("请选择一个文件进行上传");
		}
		String fileName = upFile.getFileName();

		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

		// 判断文件后缀是否合法
		if (filter != null && !filter.equals("")) {

			String[] filterSuffix = filter.split(",");
			int suffixLength = filterSuffix.length;
			boolean isLegal = false;
			for (int suffixI = 0; suffixI < suffixLength; suffixI++) {
				if (filterSuffix[suffixI].equals(suffix)) {
					isLegal = true;
				}
			}
			if (!isLegal) {
				throw new Exception("上传的文件名不合法.");
			}
		}

		// 判断文件长度是否合法
		if (fileSize > 0) {
			int upFileSize = upFile.getFileSize() / 1024;
			if (upFileSize > fileSize) {
				throw new Exception("上传的文件长度过长");
			}
		}

		// 文件真实保存路径
		String upFilePath = baseDir + "/" + upFile.getFileName();

		try {
			// 保存文件
			if (FileUploadUtil.saveFile(upFilePath, upFile.getFileData())) {
				ZipFile zipFile = new ZipFile(upFilePath);
				Enumeration enu = zipFile.entries();
				while (enu.hasMoreElements()) {
					ZipEntry entry = (ZipEntry) enu.nextElement();
					String entryName = entry.getName();

					if (entry.isDirectory()) {
						File f = new File(baseDir + "/" + entryName);
						if (!f.exists()) {
							f.mkdirs();
						}
						continue;
					} else if (!isReplace) {
						File f = new File(baseDir + "/" + entryName);
						if (f.exists()) {
							continue;
						}
					}
					BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
					int count;
					byte data[] = new byte[2048];
					FileOutputStream fos = new FileOutputStream(baseDir + "/" + entryName);
					BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
					while ((count = is.read(data, 0, 2048)) != -1) {
						dest.write(data, 0, count);
					}
					dest.flush();
					dest.close();
					is.close();
				}
			}
			return upFilePath;

		}
		catch (IOException e) {
			throw new Exception("上传文件错误,请重试");
		}
	}
}
